package topo.table;

import java.awt.Component;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;

import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableRowSorter;

import com.l2fprod.common.beans.editor.ComboBoxPropertyEditor;
import com.l2fprod.common.demo.KVTableCellRenderer;

public class ObjectTable extends JTable {
    TableMouseHandle handle;
    private ObjectTableModel mod;
    TableRowSorter sorter;

    public ObjectTable(ObjectTableModel mod) {
        super(mod);
        this.mod = mod;
        setAutoscrolls(true);
        addMouseListener(new Table_mouseAdapter());
        setSelectionMode(0);
        TableColumn col = getColumn(".ObjectIDName.");
        col.setMaxWidth(0);
        col.setMinWidth(0);
        col.setPreferredWidth(0);
        col.setResizable(false);
        col.setWidth(0);
        setRowHeight(20);
        getTableHeader().setFont(new Font("Dialog", 0, 12));

        this.sorter = new TableRowSorter(mod);
        setRowSorter(this.sorter);
    }

    public ObjectTableModel getObjectTableModel() {
        return this.mod;
    }

    public void setRendererKVType(int columnIndex, ComboBoxPropertyEditor.Value[] values) {
        String name = getColumnName(columnIndex);
        getColumn(name).setCellRenderer(new KVTableCellRenderer(values));
    }

    public void setRenderer(int columnIndex, TableCellRenderer render) {
        String name = getColumnName(columnIndex);
        getColumn(name).setCellRenderer(render);
    }

    public void setRendererIntAsBoolean(int columnIndex) {
        String name = getColumnName(columnIndex);
        ComboBoxPropertyEditor.Value[] values = new ComboBoxPropertyEditor.Value[2];
        values[0] = new ComboBoxPropertyEditor.Value(new Integer(1), "是");
        values[1] = new ComboBoxPropertyEditor.Value(new Integer(0), "否");
        getColumn(name).setCellRenderer(new KVTableCellRenderer(values));
    }

    public Object getSelectRowObject() {
        int index = getSelectedRow();
        if (index >= 0) {
            index = this.sorter.convertRowIndexToModel(index);
            return this.mod.getRowObject(index);
        }
        return null;
    }

    public List getSelectRowObjects() {
        Vector vec = new Vector();
        int[] index = getSelectedRows();
        for (int i = 0; i < index.length; i++) {
            int num = this.sorter.convertRowIndexToModel(index[i]);
            Object obj = this.mod.getRowObject(num);
            if (obj != null) {
                vec.add(obj);
            }
        }

        return vec;
    }

    void myMouseReleased(MouseEvent e) {
        if (this.handle == null) {
            return;
        }

        if (e.getClickCount() == 2) {
            this.handle.doubleClickAction(e);
        }

        if (e.getButton() == 3) {
            int point = rowAtPoint(e.getPoint());
            if (point >= 0) {
                addRowSelectionInterval(point, point);
            }

            int row = getSelectedRow();

            if (row >= 0) {
                JPopupMenu menu = this.handle.createObjectMenu(e);
                menu.show((Component) e.getSource(), e.getX(), e.getY());
            }
        }
    }

    void setMouseHandle(TableMouseHandle theHandle) {
        this.handle = theHandle;
    }

    class Table_mouseAdapter extends MouseAdapter {
        Table_mouseAdapter() {
        }

        public void mouseReleased(MouseEvent e) {
            ObjectTable.this.myMouseReleased(e);
        }
    }
}